<!--
 * @Author: hq
 * @Date: 2022-05-28 19:29:52
 * @LastEditors: hq
 * @LastEditTime: 2022-06-16 13:41:17
 * @Description: 添加物料弹窗
 * @version: 1.0
-->
<template>
  <div class="change">
    <el-row>
      <el-col :span="12">
        <el-form
          :rules="rules"
          :model="form"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="标题" prop="name">
            <el-input v-model="form.name" placeholder="请输入标题"></el-input>
          </el-form-item>
          <el-form-item label="子标题" prop="sub_name">
            <el-input
              v-model="form.sub_name"
              placeholder="请输入子标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="标记标识" prop="flag">
            <el-input
              v-model="form.flag"
              placeholder="请输入标记标识"
            ></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input
              oninput="value=value.replace(/[^\d]/g,'')"
              maxlength="3"
              v-model="form.sort"
              placeholder="请输入排序"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">确定</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { flagAction } from "@/api/sale";
export default {
  props: ["rowData"],
  data() {
    return {
      form: {
        name: "",
        sub_name: "",
        flag: "",
        sort: "",
      },
      rowId: "",
      rules: {
        name: [{ required: true, message: "请输入标题", trigger: "blur" }],
        sub_name: [
          { required: true, message: "请输入子标题", trigger: "blur" },
        ],
        flag: [{ required: true, message: "请输入标记标识", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
      },
    };
  },
  methods: {
    async flagAction(action) {
      const { err } = await flagAction({
        id: this.rowData.id,
        name: this.form.name,
        sub_name: this.form.sub_name,
        flag: this.form.flag,
        sort: this.form.sort,
        action: action,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功！",
          type: "success",
        });
        this.$emit("handleClose");
      }
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //接口
          if (this.rowData.id) {
            //编辑
            this.flagAction("MOD");
          } else {
            //新增
            this.flagAction("ADD");
          }
        }
      });
    },
  },
  created() {
    if (this.rowData.id) {
      //编辑
      this.form.name = this.rowData.name;
      this.form.sub_name = this.rowData.sub_name;
      this.form.flag = this.rowData.flag;
      this.form.sort = this.rowData.sort;
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
